import wx
import os 

from tables import *
import numpy as N


def File_Dialog():    
# setup the GUI main loop
    app = wx.PySimpleApp()
    filename = wx.FileSelector(message='Select File to Save', default_path=os.getcwd(), parent=None)
    print filename
    return filename


def save_array(numpy_array, filename):
    
    if type(numpy_array) is not N.ndarray:
        print "Incorrect data type submitted, a Numpy array was not provided"        
    else:
        hdf = openFile(filename, mode = "w", title = 'Data_Array')
        varGroup = hdf.createGroup("/", 'Data_Arrays', 'Data_Arrays')
        hdf.createArray(varGroup, "Array", numpy_array)        
        hdf.close()      
          

def load_array(filename):
    if os.path.isfile(filename):
        loadedVars = []
        hdf = openFile(filename, mode = "r")
        groupDict = hdf.root._v_groups
        if groupDict.has_key('Data_Arrays'):
            for node in hdf.root.Data_Arrays._f_iterNodes():
                if node._c_classId is 'ARRAY':
                    loadedVars.append((node._v_hdf5name,node.read()))
                    
                    
        hdf.close()
        return dict(loadedVars)


if __name__ == "__main__":
    
    x = N.arange(0,1000, N.random.random())
    y = N.cos(x-N.random.random())
    #save_array(y, File_Dialog())
    data = load_array(File_Dialog())
    for value in data.values():
        print type(value)
        print value


            